
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>CSS <span class="color_h1">创建</span>
</h1>
<hr/>
<p class="intro">当读到一个样式表时，浏览器会根据它来格式化 HTML 文档。</p>
<hr/>
<h2>如何插入样式表</h2>
<p>插入样式表的方法有三种:</p>
<ul>
<li>外部样式表(External style sheet)</li>
<li>内部样式表(Internal style sheet)</li>
<li>内联样式(Inline style)</li>
</ul>
<hr/>
<h2>外部样式表</h2>
<p>当样式需要应用于很多页面时，外部样式表将是理想的选择。在使用外部样式表的情况下，你可以通过改变一个文件来改变整个站点的外观。每个页面使用  &lt;link&gt; 标签链接到样式表。 &lt;link&gt; 标签在（文档的）头部：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">link</span><span class="hl-code"> </span><span class="hl-var">rel</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">stylesheet</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text/css</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">mystyle.css</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<p>浏览器会从文件 mystyle.css 中读到样式声明，并根据它来格式文档。</p>
<p>外部样式表可以在任何文本编辑器中进行编辑。文件不能包含任何的 html 标签。样式表应该以 .css 扩展名进行保存。下面是一个样式表文件的例子：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">hr</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-reserved">color:</span><span class="hl-code">sienna</span><span class="hl-reserved"></span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">p</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-reserved">margin-left:</span><span class="hl-number">20</span><span class="hl-string">px</span><span class="hl-reserved"></span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">body</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-reserved">background-image:</span><span class="hl-code">url</span><span class="hl-code">("/</span><span class="hl-code">images</span><span class="hl-code">/</span><span class="hl-code">back40</span><span class="hl-code">.</span><span class="hl-code">gif</span><span class="hl-code">")</span><span class="hl-reserved"></span><span class="hl-code">;</span><span class="hl-brackets">}</span></div>
</div>
</div>
<blockquote>
<p><img alt="Remark" decoding="async" src="/images/lamp.gif"/> 不要在属性值与单位之间留有空格（如："margin-left: 20 px" ），正确的写法是 "margin-left: 20px" 。</p></blockquote>
<hr/>
<h2>内部样式表</h2>
<p>当单个文档需要特殊的样式时，就应该使用内部样式表。你可以使用  &lt;style&gt; 标签在文档头部定义内部样式表，就像这样:</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<br/><hr/>
<h2>内联样式</h2>
<p>由于要将表现和内容混杂在一起，内联样式会损失掉样式表的许多优势。请慎用这种方法，例如当样式仅需要在一个元素上应用一次时。</p>
<p>要使用内联样式，你需要在相关的标签内使用样式（style）属性。Style 属性可以包含任何 CSS 属性。本例展示如何改变段落的颜色和左外边距：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-code"> </span><span class="hl-var">style</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">color:sienna;margin-left:20px</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">这是一个段落。</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<br/><hr/>
<h2>多重样式</h2>
<p>如果某些属性在不同的样式表中被同样的选择器定义，那么属性值将从更具体的样式表中被继承过来。 </p>
<p>例如，外部样式表拥有针对 h3 选择器的三个属性：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">h3</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">color:</span><span class="hl-var">red</span><span class="hl-reserved"></span><span class="hl-code">;
    </span><span class="hl-reserved">text-align:</span><span class="hl-string">left</span><span class="hl-reserved"></span><span class="hl-code">;
    </span><span class="hl-reserved">font-size:</span><span class="hl-number">8</span><span class="hl-string">pt</span><span class="hl-reserved"></span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>而内部样式表拥有针对 h3 选择器的两个属性：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">h3</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">text-align:</span><span class="hl-string">right</span><span class="hl-reserved"></span><span class="hl-code">;
    </span><span class="hl-reserved">font-size:</span><span class="hl-number">20</span><span class="hl-string">pt</span><span class="hl-reserved"></span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>假如拥有内部样式表的这个页面同时与外部样式表链接，那么 h3 得到的样式是：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">color</span><span class="hl-special">:red</span><span class="hl-code">;
</span><span class="hl-identifier">text-align</span><span class="hl-special">:right</span><span class="hl-code">;
</span><span class="hl-identifier">font-size</span><span class="hl-code">:20</span><span class="hl-identifier">pt</span><span class="hl-code">;</span></div>
</div>
</div>
<p>即颜色属性将被继承于外部样式表，而文字排列（text-alignment）和字体尺寸（font-size）会被内部样式表中的规则取代。</p>
<hr/>
<h2>多重样式优先级</h2>
<p>样式表允许以多种方式规定样式信息。样式可以规定在单个的 HTML 元素中，在 HTML 页的头元素中，或在一个外部的 CSS 文件中。甚至可以在同一个 HTML 文档内部引用多个外部样式表。</p>
<p>
一般情况下，优先级如下：
</p><p><b>
（内联样式）Inline style &gt; （内部样式）Internal style sheet &gt;（外部样式）External style sheet  &gt; 浏览器默认样式</b></p>
<p><a  rel="noopener" target="_blank">style.css</a> 文件样式代码如下：</p>
<pre>h3 {
    color:blue;
}</pre>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-comment">&lt;!--</span><span class="hl-comment"> 外部样式 style.css </span><span class="hl-comment">--&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">link</span><span class="hl-code"> </span><span class="hl-var">rel</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">stylesheet</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text/css</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">style.css</span><span class="hl-quotes">"</span><span class="hl-brackets">/&gt;</span><span class="hl-code">
</span><span class="hl-comment">&lt;!--</span><span class="hl-comment"> 设置：h3{color:blue;} </span><span class="hl-comment">--&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">style</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text/css</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
      /* 内部样式 */
      h3{color:green;}
    </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h3</span><span class="hl-brackets">&gt;</span><span class="hl-code">显示绿色，是内部样式</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h3</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span></div>
</div>
<br/>
<a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p><strong>注意：</strong>如果外部样式放在内部样式的后面，则外部样式将覆盖内部样式，实例如下：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-comment">&lt;!--</span><span class="hl-comment"> 设置：h3{color:blue;} </span><span class="hl-comment">--&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">style</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text/css</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
      /* 内部样式 */
      h3{color:green;}
    </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-comment">&lt;!--</span><span class="hl-comment"> 外部样式 style.css </span><span class="hl-comment">--&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">link</span><span class="hl-code"> </span><span class="hl-var">rel</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">stylesheet</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">text/css</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">style.css</span><span class="hl-quotes">"</span><span class="hl-brackets">/&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h3</span><span class="hl-brackets">&gt;</span><span class="hl-code">显示蓝色，是外部样式</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h3</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span></div>
</div>
<br/>
<a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div> <div class="scroll-top"><a >返回顶部</a></div>
</div>

    </body>
    </html>
    